package com.polaris.live.popup

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.adapter.GiftRecordAdapter
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.PopupGiftrecordBinding
import com.polaris.live.net.userService
import com.polaris.live.translation.R.string
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.widget.refresh.EmptyView

/**
 *
 * 获赠记录
 * @author Created by 天晴 on 2024/7/17 16:24
 * @since 1.0.0
 **/
class GiftRecordPopup(
    context: Context,
    val mViewModel: BaseViewModel,
    val backpackId: Int,
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupGiftrecordBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_giftrecord
    }

    private val mAdapter by lazy { GiftRecordAdapter() }

    private val emptyView by lazy {
        EmptyView(context).apply {
            setEmptyTip(R.drawable.family_empty, StringUtils.getString(string.no_hava_data))
        }
    }

    override fun onCreate() {
        mBinding = PopupGiftrecordBinding.bind(popupImplView)
        mBinding.refresh.setOnLoadMoreListener {
            getData(++pageNumber)
        }
        mBinding.refresh.setOnRefreshListener {
            pageNumber = 0
            getData(pageNumber)
        }
        mAdapter.setEmptyView(emptyView)
        mBinding.recycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        }
        getData(pageNumber)

        mAdapter.setOnItemChildClickListener { _, view, position ->
            val item = mAdapter.data[position]
            when (view.id) {
                R.id.user_ll -> {
                    Router.builder(RoutePath.MESSAGE_ACTIVITY)
                        .withInt("page", UserConst.From.SINGLE)
                        .apply {
                            item.userBasic.also {
                                withString("nickname", it.nickname)
                                withString("avatar", it.avatar)
                                it.avatarFrame?.also { af -> withString("avatarFrame", af) }
                                it.chatBubble?.also { cb -> withString("chatBubble", cb) }
                            }
                        }
                        .withLong("userId", item.userBasic.userId)
                        .navigation(context)
                }

                R.id.time_ll -> {
                    GiftRecordTipsPopup.show(context, view, item.getType())
                }
            }
        }
    }

    private var pageNumber = 0
    private val pageSize = 20

    fun getData(pageNumber: Int) {
        mViewModel.requestLoading({
            userService.backpackReceives(backpackId, pageNumber, pageSize)
        }, {
            if (pageNumber == 0) {
                mAdapter.data.clear()
                mAdapter.setList(it.list)
            } else {
                mAdapter.addData(it.list)
            }

            if (pageNumber == 0) {
                mBinding.refresh.finishRefresh()
            } else {
                mAdapter.loadMoreModule.loadMoreComplete()
            }
            mAdapter.loadMoreModule.isEnableLoadMore = it.hasMore
        }, {
            if (pageNumber == 0) {
                mBinding.refresh.finishRefresh(false)
                mAdapter.loadMoreModule.isEnableLoadMore = false
            } else {
                mAdapter.loadMoreModule.loadMoreFail()
            }
            if (mAdapter.data.isNotEmpty()) {
                ToastGravity.showGravity(it.errorMsg)
            }
        })

    }


    companion object {

        fun show(context: Context, mViewModel: BaseViewModel, backpackId: Int) {
            XPopup.Builder(context)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .enableDrag(false)
                .asCustom(GiftRecordPopup(context, mViewModel, backpackId))
                .show()
        }
    }
}